package zhou.cutfruit.thread;

import java.util.Random;

import android.content.Context;
import android.graphics.Matrix;
import android.util.Log;
import zhou.cutfruit.models.Movable;
import zhou.cutfruit.util.ConstantNumber;

public class FruitThread extends Thread {
	Movable fruit; // 该线程对应的水果
	boolean flag;// 线程执行标志位
	int sleepSpan = 10;// 休息间隔
	float g = 1500;// 重力加速度
	double currentTime; // 当前时间
	Random random = new Random();
	int count = 0; // 循环次数

	boolean isFlying = false;

	Context context;
	Matrix matrix = new Matrix();
	
	int timeCount = 0 ;
	
	

	// Display display ;

	public FruitThread(Movable fruit, Context context) {
		this.fruit = fruit;
		this.flag = true;
		this.context = context;

	}

	// 根据物理公式动态改变水果的速度、位置
	public void run() {

		while (flag) {

			/*
			 * MotionEvent event = null;
			 * 
			 * event = fruit.getEvent();
			 */
			
		/*matrix.setRotate(20, (fruit.getX()+fruit.getWidth())/2, fruit.getY()+fruit.getHeight()/2);
		Bitmap bitmap = Bitmap.createBitmap(fruit.getBitmap(), fruit.getX(), fruit.getY(), (int)fruit.getWidth(),(int)fruit.getHeight(), matrix,false);
		fruit.getBitmap().recycle();
		fruit.setBitmap(null);
		fruit.setBitmap(bitmap);*/
			timeCount++ ;
			if (timeCount%10 == 0) {
				try {
					if (fruit.getBitmap_index()>=fruit.getFruitBimtapSize()-1) {
						fruit.setBitmap_index(0);
					}
					else {
						fruit.setBitmap_index(fruit.getBitmap_index()+1);
					}
				} catch (Exception e) {
					fruit.setBitmap_index(0);
				}
			}
			
			
			
			
			
			
			fruit.setX(fruit.getX() + (int) fruit.getVx());
			fruit.setY(fruit.getY() + (int) (fruit.getVy() * sleepSpan / 1000));
			fruit.setVy(fruit.getVy() + g * sleepSpan / 1000);

			/*
			 * if (event != null) {
			 * 
			 * 
			 * 
			 * 
			 * int distance = 99999 ; int event_X = (int)event.getX(); int
			 * event_y = (int)event.getY(); //最简化的处理，将两边之和作为距离 distance =
			 * event_X-fruit.getX() + event_y-fruit.getY();
			 * 
			 * Log.i("touch",
			 * "distance="+distance+"   eventX="+event_X+"    event_y="
			 * +event_y);
			 * Log.i("touch","fruit_X = "+fruit.getX()+"   fruit_y ="+
			 * fruit.getY()+"   fruit_R="+fruit.getR() );
			 * 
			 * 
			 * if (distance<=fruit.getR()) {
			 * Log.i("zhou","fruit.R = "+fruit.getR());
			 * //fruit.judgeCutting(10000, 10000); fruit.setCuted(true); } }
			 */

			if (fruit.getX() >= ConstantNumber.SCREEN_WIDTH
					|| fruit.getY() > 700) {
				count++;
				if (fruit.isCuted() == true) {
					fruit.setCuted(false);
				}

				if (fruit.getOffset_whenCuted() != 10) {
					fruit.setOffset_whenCuted(10);
				}

				try {
					Thread.sleep(1000 + random.nextInt(1000));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				fruit.setVx(fruit.getStart_vx());
				fruit.setVy(fruit.getStart_vy());

				
				int ran = random.nextInt(ConstantNumber.SCREEN_WIDTH - 200);
				fruit.setCuted(false);
				fruit.setX(ran + 100);
				if (ran + 200 > ConstantNumber.SCREEN_WIDTH / 2) {
					fruit.setVx(-fruit.getStart_vx());
				}

				fruit.setY(fruit.getStart_y());
				isFlying = false;
			}

			if (!isFlying && fruit.getY() <= 640) {
				Log.i("zhou", "飞出来了" + fruit.getY() + "  " + fruit.getX());

				fruit.setPlaySound(true);
				fruit.setStartFlying(true);
				isFlying = true;
				
			}

			try {
				Thread.sleep(sleepSpan);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			/*
			 * if (count==1||count==2) { fruit.setCuted(true); } else {
			 * fruit.setCuted(false); }
			 */

			if (count >= 15) {
				//flag = false;
			}
			

		}

	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	
	
}
